package cn.gok.hotelback.service.impl;

import cn.gok.hotelback.entity.User;
import cn.gok.hotelback.mapper.UserMapper;
import cn.gok.hotelback.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User checkLogin(String username, String password) {
        User user = userMapper.findByUsername(username);
        if (user != null && user.getPassword().equals(password)) {
            return user;
        }
        return null;
    }

    @Override
    public boolean register(User user) {
        // 检查用户名是否已存在
        User existingUser = userMapper.findByUsername(user.getUsername());
        if (existingUser != null) {
            return false; // 用户名已存在
        }
        // 注册时level默认赋值为1
        if (user.getLevel() == null) {
            user.setLevel(1);
        }
        // 插入新用户
        return userMapper.insertUser(user) > 0;
    }

    @Override
    public java.util.List<User> findAllMembers() {
        return userMapper.findAllMembers();
    }
    @Override
    public boolean setUserLevel(Integer userId, Integer level) {
        return userMapper.updateUserLevel(userId, level) > 0;
    }
} 